package gw.com.android.ui.dialog;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.gwtsz.chart.output.utils.GTTKDataManager;
import com.gwtsz.gts2.hx.R;

import gw.com.android.app.ActivityManager;
import gw.com.android.app.AppMain;
import gw.com.android.app.GTConfig;
import gw.com.android.terminal.AppTerminal;
import www.com.library.util.DeviceUtil;
import www.com.library.view.BtnClickListener;

/**
 * 创建一般的提示弹层的对象,只带确定按钮的提示信息
 * 
 * @author Reeta.zou
 * 
 */
public class PopupConfirmDialog extends BaseDialog {

	public PopupConfirmDialog(Context context) {
		super(context);
	}

	private String mTitleText;
	private String mContentText;
	private int contentGravity = Gravity.CENTER;
	public boolean isClickClose = true;//标识是否点击按钮后，弹框消失

	/**
	 * 自带默认的点击事件，点击弹层消失,按钮文字为“确定”
	 * 
	 * @param act
	 * @param titleID
	 * @param contentID
	 *            错误提示信息
	 * @return
	 */
	public static PopupConfirmDialog newInstance(Activity act, int titleID, int contentID) {
		return newInstance(act, titleID, contentID, 0);
	}

	/**
	 * 自带默认的点击事件，点击弹层消失
	 * 
	 * @param act
	 * @param titleID
	 *            title提示信息
	 * @param contentID
	 * @param posTitleID
	 * @return
	 */
	public static PopupConfirmDialog newInstance(Activity act, int titleID, int contentID, int posTitleID) {
		String title = "";
		String content = "";
		String posTitle = "";
		if (titleID != 0) {
			title = AppMain.getAppString(titleID);
		}
		if (contentID != 0) {
			content = AppMain.getAppString(contentID);
		}
		if (posTitleID != 0) {
			posTitle = AppMain.getAppString(posTitleID);
		}
		return newInstance(act, title, content, posTitle);
	}

	/**
	 * 自带默认的点击事件，点击弹层消失，按钮文字为“确定”
	 * 
	 * @param act
	 * @param title
	 * @param content
	 *            错误提示信息
	 * @return
	 */
	public static PopupConfirmDialog newInstance(Activity act, String title, String content) {
		return newInstance(act, title, content, "");
	}

	/**
	 * 自带默认的点击事件，点击弹层消失，按钮文字为“确定”
	 * @param act
	 * @param content  错误提示信息
	 * @return
	 */
	public static PopupConfirmDialog newInstance(Activity act, String content) {
		return newInstance(act, "",content, "");
	}

	/**
	 * 自带默认的点击事件，点击弹层消失
	 * 
	 * @param act
	 * @param title
	 * @param content
	 *            错误提示信息
	 * @param posTitle
	 *            按钮文字
	 * @return
	 */
	public static PopupConfirmDialog newInstance(Activity act, String title, String content, String posTitle) {
		return newInstance(act,title,content,posTitle,Gravity.NO_GRAVITY,null);
	}

	/**
	 * 设置弹层的点击事件，按钮文字为“确定”
	 * 
	 * @param act
	 * @param contentID
	 * @param mBtnClickListener
	 * @return
	 */
	public static PopupConfirmDialog newInstance(Activity act, int contentID, BtnClickListener mBtnClickListener) {
		String content = "";
		if (contentID != 0) {
			content = AppMain.getAppString(contentID);
		}
		return newInstance(act, content, mBtnClickListener);
	}

	/**
	 * 设置弹层的点击事件，按钮文字为“确定”
	 * 
	 * @param act
	 * @param mBtnClickListener
	 * @return
	 */
	public static PopupConfirmDialog newInstance(Activity act, String content, BtnClickListener mBtnClickListener) {
		return newInstance(act, "", content, mBtnClickListener);
	}

	/**
	 * 设置弹层的点击事件，按钮文字为“确定”
	 * 
	 * @param act
	 * @param mBtnClickListener
	 * @return
	 */
	public static PopupConfirmDialog newInstance(Activity act, String title, String content, BtnClickListener mBtnClickListener) {
		return newInstance(act,title,content,"",Gravity.NO_GRAVITY,mBtnClickListener);
	}

	public static PopupConfirmDialog newInstance(Activity act, String title, String content, String posTitle, int contentGravity, BtnClickListener mBtnClickListener) {
		PopupConfirmDialog dialog = new PopupConfirmDialog(act);
		if(null != mBtnClickListener){
			dialog.setBtnClickListener(mBtnClickListener);
		}

		if (null != title && !title.equals("")) {
			dialog.mTitleText = title;
		}

		if (null != posTitle && !"".equals(posTitle)) {
			dialog.setPositiveBtnText(posTitle, true);
		}

		if (null != content && !content.equals("")) {
			dialog.mContentText = content;
			dialog.contentGravity = contentGravity;
		}
		int padding = (DeviceUtil.instance().getScreenPixelsWidth(act) - (int) (280 * DeviceUtil.instance().getScreenDensity(act))) / 2;
		dialog.setPadding(padding, 0, padding, 0);
		mInstance = dialog;
		return dialog;
	}

	/**
	 * 错误信息提示,按钮文字为“返回”,显示失败图标和title栏,主要用于交易界面提示信息
	 * 
	 * @param msg
	 */
	public static void NotifyPrompt(Activity act, String msg) {
		final PopupConfirmDialog dialog = PopupConfirmDialog.newInstance(act, "", msg);
		dialog.setPositiveBtnText(AppMain.getAppString(R.string.btn_back), true);
		mInstance = dialog;
		dialog.show();
	}

	@Override
	public void initParam() {
		mCustomViewResId = R.layout.dialog_action_content;
		mHasPosButton = true;
		mHasNegButton = false;
	}

	@Override
	public void inflaterCustomView(View mCustomView) {
		TextView contentV = (TextView) mCustomView.findViewById(R.id.dialog_content_text);
		TextView titleV = (TextView) mCustomView.findViewById(R.id.dialog_title);
		View divider = mCustomView.findViewById(R.id.divider_view);

		if (titleV != null) {
			if (null != mTitleText && mTitleText.length() > 0) {
				titleV.setText(mTitleText);
				titleV.setVisibility(View.VISIBLE);
				divider.setVisibility(View.VISIBLE);
			} else {
				titleV.setVisibility(View.GONE);
				divider.setVisibility(View.GONE);
			}
		}

		if (contentV != null) {
			contentV.setText(mContentText);
//			contentV.setGravity(contentGravity);
		}

		divider.setVisibility(View.GONE);
		mBtnPos.setBackgroundResource(R.drawable.dialog_btn_bg_selector);
		// 不设置点击事件时使用默认的点击事件
		mBtnPos.setOnClickListener(this);
	}
	
	@Override
	public void dismiss() {
        mInstance = null;
		
		if (mPreDismissListener != null){
			mPreDismissListener.onPreDismiss(this);
		}
        if(isClickClose){
        	super.dismiss();
        }

		if (mDimissedListener != null){
			mDimissedListener.onDismissed(this);
		}
	}

	/**
	 * 被迫登出的弹层
	 * @param mOwnerAct
	 * @return
	 */
	public static PopupConfirmDialog getKickOut(final Activity mOwnerAct, String  mContent){
		PopupConfirmDialog dialog = PopupConfirmDialog.newInstance(mOwnerAct, mContent, new BtnClickListener() {

			@Override
			public void onBtnClick(int viewID) {
				if (viewID == R.id.action_btn_pos) {
					GTConfig.instance().setBooleanValue(GTConfig.PREF_AUTO_LOGIN,false);
					GTConfig.instance().mCurLoginPhone="";
					GTConfig.instance().isPhoneLogin =false;
					GTConfig.instance().mHasKickOut = false;

					GTConfig.instance().mUserPwd = "";
					GTTKDataManager.instance().clearTimeData();
					GTTKDataManager.instance().clearKData();
					GTTKDataManager.instance().clearIndictor();
					AppTerminal.instance().clearAllChartCache();
					ActivityManager.backLogin(mOwnerAct, true);
				}
			}
		});
		dialog.setCancelable(false);
		dialog.show();
		mInstance = dialog;
		return dialog;
	}

}
